home *** CD-ROM | disk | FTP | other *** search
/ The Fatted Calf / The Fatted Calf.iso / Modules / BackSpaceModules / Source / AquariumView / FishBrain.m < prev    next >
Text File  |  1993-03-12  |  2KB  |  134 lines

  1. #import "FishBrain.h"
  2. #import "RotImage.h"
  3. #ifndef NS3x
  4. #import "Thinker.h"
  5. #else
  6. #import "Thinker3.h"
  7. #endif
  8.  
  9. #import <libc.h>
  10.  
  11. @implementation FishBrain
  12.  
  13.  
  14. - changeXDirection
  15. {
  16.     direction = direction ^ 
  17.         (((direction & RIGHT) == RIGHT) ?  RIGHT : LEFT);
  18.     xspeed = (float)(random() % MAX_X_SPEED) / 10.0 + .1;
  19.     [body flip:HORIZONTAL];
  20.     return self;
  21. }
  22.  
  23. - changeYDirection
  24. {
  25.     direction = direction ^ (((direction & UP) == UP) ? UP : DOWN);
  26.      yspeed = (float)(random() % MAX_Y_SPEED) / 10.0 + .1;
  27.     return self;
  28. }
  29.  
  30. -init:(NXSize *)drawingSize fishSize:(NXSize *)mysize owner:mybody
  31. {
  32.  
  33.     body = mybody;
  34.     WIDTH = mysize->width;
  35.     HEIGHT = mysize->height;
  36.  
  37.     sizeOfAquar = *drawingSize;
  38.  
  39.     srandom(currentTimeInMs());
  40.  
  41.     xspeed = (float)(random() % MAX_X_SPEED) / 10.0 + .1;
  42.     yspeed = (float)(random() % MAX_Y_SPEED) / 10.0 + .1;
  43.  
  44.     xpos = random() % (int)(sizeOfAquar.width-WIDTH);
  45.     ypos = random() % (int)(sizeOfAquar.height-HEIGHT);
  46.  
  47.  
  48.     direction = direction ^ (random() % 2) ? RIGHT : LEFT;
  49.     direction = direction ^ (random() % 2) ? UP : DOWN;
  50.     
  51.     if ((direction & LEFT) == LEFT) [self changeXDirection];
  52.  
  53.     return self;
  54. }
  55.  
  56. -(float)getXPos 
  57. {
  58.     if ((direction & LEFT) == LEFT) {
  59.         if (xpos < 0.0) {
  60.             xpos = 0.0;
  61.             [self changeXDirection];
  62.             return (xpos += xspeed);
  63.         } else {
  64.             return (xpos -= xspeed);
  65.         }
  66.     } else {
  67.         if (xpos > (sizeOfAquar.width - (WIDTH + (MAX_X_SPEED/2)))) {
  68.             xpos = sizeOfAquar.width - (WIDTH + (MAX_X_SPEED/2));
  69.             [self changeXDirection];
  70.             return (xpos -= xspeed);
  71.         } else {
  72.             return (xpos += xspeed);
  73.         }
  74.     }
  75.     return xpos;
  76. }
  77.  
  78. -(float)getYPos
  79. {
  80.     if ((direction & DOWN) == DOWN) {
  81.         if (ypos < 0.0) {
  82.             ypos = 0.0;
  83.             [self changeYDirection];
  84.         } else {
  85.             return (ypos -= yspeed);
  86.         }
  87.     } else {
  88.         if (ypos > (sizeOfAquar.height - (HEIGHT + (MAX_Y_SPEED/2)))) {
  89.             ypos = sizeOfAquar.height - (HEIGHT + (MAX_Y_SPEED/2));
  90.             [self changeYDirection];
  91.         } else {
  92.             return (ypos += yspeed);
  93.         }
  94.     }
  95.     return ypos;
  96. }
  97.  
  98. -(float)getXSpeed 
  99. {
  100.     return xspeed;
  101. }
  102.  
  103. -(float)getYSpeed
  104. {
  105.     return yspeed;
  106. }
  107.  
  108. -(float)getAngle 
  109. {
  110.     return angle;
  111. }
  112.  
  113. -(int)getDirection 
  114. {
  115.     return direction;
  116. }
  117.  
  118. -viewDidResize:(NXSize *)newSize
  119. {
  120.     sizeOfAquar = *newSize;
  121.     return self;
  122. }
  123.  
  124. -(NXPoint)getCurrentPoint 
  125. {
  126.     NXPoint tmp;
  127.  
  128.     tmp.x = xpos;
  129.     tmp.y = ypos;
  130.     return tmp;
  131. }
  132.  
  133. @end
  134.